package com.soul.dj.ui.activity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Looper;
import android.os.Vibrator;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.google.zxing.client.result.ParsedResult;
import com.mylhyl.zxing.scanner.OnScannerCompletionListener;
import com.mylhyl.zxing.scanner.ScannerOptions;
import com.mylhyl.zxing.scanner.ScannerView;
import com.soul.dj.MainApplication;
import com.soul.dj.R;
import com.soul.dj.util.Const;
import com.soul.dj.util.http.JSONCallBack;
import com.soul.dj.util.http.MediaType;
import com.soul.dj.util.http.json.JsonProcessUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OptionsScannerActivity extends BaseActivity implements OnScannerCompletionListener {

    private Map<String,Object> infoMap = new HashMap<>();

    private ScannerView mScannerView;

    @Override
    protected void initView() {

        try{
            String tmp = getIntent().getStringExtra("areaMap");
            infoMap = JsonProcessUtil.jsonToBean2(tmp,Map.class);
        }catch (Exception e){

        }

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.scanner_activity);


        mScannerView = (ScannerView) findViewById(R.id.scanner_view);

        mScannerView.setOnScannerCompletionListener(this);

        findViewById(R.id.finish).setOnClickListener(this);
        ((TextView)findViewById(R.id.title)).setText("扫描二维码");



//        mScannerView.toggleLight(true);

        ScannerOptions.Builder builder = new ScannerOptions.Builder();
        builder
                .setFrameStrokeColor(Color.RED)
                .setFrameStrokeWidth(1.5f)
//                .setFrameSize(256, 256)
//                .setFrameCornerLength(22)
//                .setFrameCornerWidth(2)
//                .setFrameCornerColor(0xff06c1ae)
//                .setFrameCornerInside(true)

//                .setLaserLineColor(0xff06c1ae)
//                .setLaserLineHeight(18)

//                .setLaserStyle(ScannerOptions.LaserStyle.RES_LINE,R.mipmap.wx_scan_line)

//                .setLaserStyle(ScannerOptions.LaserStyle.RES_GRID, R.mipmap.zfb_grid_scan_line)//网格图
//                .setFrameCornerColor(0xFF26CEFF)//支付宝颜色

//                .setScanFullScreen(true)

//                .setFrameHide(false)
//                .setFrameCornerHide(false)
//                .setLaserMoveFullScreen(false)

                .setViewfinderCallback(new ScannerOptions.ViewfinderCallback() {
                    @Override
                    public void onDraw(View view, Canvas canvas, Rect frame) {
//                        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.connect_logo);
//                        canvas.drawBitmap(bmp, frame.right / 2, frame.top - bmp.getHeight(), null);
                    }
                })

                .setScanMode(BarcodeFormat.QR_CODE)
                .setTipText("扫描二维码")
                .setTipTextSize(19)
                .setTipTextColor(getResources().getColor(R.color.main_menu_txt))
//                .setCameraZoomRatio(2)
        ;

        mScannerView.setScannerOptions(builder.build());
    }


    @Override
    protected void onResume() {
        mScannerView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mScannerView.onPause();
        super.onPause();
    }

    @Override
    public void onScannerCompletion(Result rawResult, ParsedResult parsedResult, Bitmap barcode) {
//        Toast.makeText(this, rawResult.getText(), Toast.LENGTH_SHORT).show();

        String resultCode = rawResult.getText().substring(rawResult.getText().indexOf("=")+1);
//        toast(resultCode +"_" +infoMap.get("MAINID").toString()+infoMap.get("CHECKAREAID").toString());
        if (!TextUtils.equals(infoMap.get("MAINID").toString()+infoMap.get("CHECKAREAID").toString(),resultCode)){
            toast("此二维码不匹配");
            vibrate();
            mScannerView.restartPreviewAfterDelay(0);
            return;
        }


        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("mainid", infoMap.get("MAINID"));
        paramsMap.put("checkareaid", infoMap.get("CHECKAREAID"));
        paramsMap.put("poslatitude", MainApplication.getMap(Const.LATITUDE));
        paramsMap.put("poslongitude", MainApplication.getMap(Const.LONGITUDE));
//        sendHttp(Const.REQUEST_URL_GETUNCHECKAREALIST, MediaType.JSON,paramsMap,new JSONCallBack<List>(this,"isQRCode", List.class));

    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.finish:
                MainApplication.removeActivity(this);
                break;
        }
    }

    @Override
    public void onAjaxFail(String code, String title, String str, String tag) {
        super.onAjaxFail(code, title, str, tag);
        Looper.prepare();
        vibrate();
        mScannerView.restartPreviewAfterDelay(0);
        Looper.loop();
    }

    @Override
    public void onAjaxOK(Object result, String tag) {
        super.onAjaxOK(result, tag);
        if (TextUtils.equals("isQRCode",tag)){

//            Intent intent = new Intent(this, UnCheckPartListActivity.class);
//            intent.putExtra("areaMap", getIntent().getStringExtra("areaMap"));
//            startActivity(intent);
        }
    }

}